﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FrankensteamClient
{
    /// <summary>
    /// Interaction logic for ChatWindow.xaml
    /// </summary>
    public partial class ChatWindow : Window
    {
        TabHandler newTabItem;

        public string CurrentTab
        {
            get
            {
                if (newTabItem.Title != "USER")
                    return ((TabItem)ChatTabControl.SelectedItem).Header.ToString();
                else
                    return "ALL";
            }
        }

        public ChatWindow()
        {
            InitializeComponent();
            ClientAPI.Model.Instance._updateChatHandler += UpdateChatPage;
            ClientAPI.Model.Instance._currentChatsHandler += UpdateCurrentChats;
            ClientAPI.Model.Instance._chatListHandler += UpdateChatList;

            newTabItem = new TabHandler();

            ClientAPI.Model.Instance.ChatMessageReceived();
            ClientAPI.Model.Instance.UpdateChatList();
        }

        private void MinimiseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void CloseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MaximiseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;
            Button MaximiseButton = (Button)sender;
            MaximiseButton.Click -= new RoutedEventHandler(NormaliseWindow_Click); //set it to null from last usage
            MaximiseButton.Content = "o";
            MaximiseButton.Click += new RoutedEventHandler(NormaliseWindow_Click);
        }

        private void NormaliseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            Button MaximiseButton = (Button)sender;
            MaximiseButton.Click -= new RoutedEventHandler(NormaliseWindow_Click); //set it to null from last usage
            MaximiseButton.Content = "O";
            MaximiseButton.Click += new RoutedEventHandler(MaximiseWindow_Click);
        }

        private void MenuBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void btnPrivateChat_Click(object sender, RoutedEventArgs e)
        {

            ListBoxItem userName = lstOnlineUsers.SelectedItem as ListBoxItem;

            if (userName != null)
            {
                newTabItem.Title = userName.Content.ToString();
                ChatTabControl.Items.Add(newTabItem);
                newTabItem.Focus();
            }
            else
            {

                MessageBox.Show("Error! Please select a username.");
            }
        }

        private void UpdateChatPage(object sender, UpdateChatEventArgs eargs)
        {
            if (!lstChatRoom.Dispatcher.CheckAccess())
            {
                lstChatRoom.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(CurrentTab);
                })
                );
            }
            else
                lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(CurrentTab);
        }

        private void UpdateCurrentChats(object sender, UpdateCurrentChatsEventArgs eargs)
        {
            if (!newTabItem.ContentInstance.lstChatRoom.Dispatcher.CheckAccess())
            {
                newTabItem.ContentInstance.lstChatRoom.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    newTabItem.ContentInstance.lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(CurrentTab);
                })
                );
            }
            else
            {
                newTabItem.ContentInstance.lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(CurrentTab);
            }
        }

        private void UpdateChatList(object sender, UpdateChatListEventArgs eargs)
        {
            if (!lstOnlineUsers.Dispatcher.CheckAccess())
                lstOnlineUsers.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    lstOnlineUsers.Items.Clear();
                    foreach (string i in ClientAPI.Model.Instance.GetChatList())
                    {
                        lstOnlineUsers.Items.Add(i);
                    }
                })
                );
            else
            {
                lstOnlineUsers.Items.Clear();
                foreach (string i in ClientAPI.Model.Instance.GetChatList())
                {
                    lstOnlineUsers.Items.Add(i);
                }
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (ClientAPI.Model.Instance.IsConnected())
                {
                    if (txtChat.Text != "" || txtChat.Text != null)
                    {
                        ClientAPI.Model.Instance.SendChatMessage(CurrentTab, txtChat.Text);
                    }

                    txtChat.Text = "";
                }
                else
                    MessageBox.Show("warning not connected", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void btnSendChat_Click(object sender, RoutedEventArgs e)
        {
            if (ClientAPI.Model.Instance.IsConnected())
            {
                if (newTabItem.Title != null)
                {
                    if (txtChat.Text != "" || txtChat.Text != null)
                    {
                        ClientAPI.Model.Instance.SendChatMessage(CurrentTab, txtChat.Text);
                    }
                }
                txtChat.Text = "";
            }
            else
                MessageBox.Show("warning not connected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}

